﻿using System;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryEmployee : ADONetRepositoryBase<Employee> 
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Employee entity)
        {
            var cmd = new SqlCommand("EmployeeInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Surname", entity.Surname));
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("Middlename", entity.Middlename));
            cmd.Parameters.Add(new SqlParameter("Post", entity.Post));
            cmd.Parameters.Add(new SqlParameter("AddressId", entity.Address.Id));
            logger.Trace("SqlCommand для записи данных Сотрудника в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("EmployeeDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Сотрудника id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Employee entity)
        {
            var cmd = new SqlCommand("EmployeeUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("Surname", entity.Surname));
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("Middlename", entity.Middlename));
            cmd.Parameters.Add(new SqlParameter("Post", entity.Post));
            cmd.Parameters.Add(new SqlParameter("AddressId", entity.Address.Id));
            logger.Trace("SqlCommand для обновления данных Сотрудника id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetEmployee", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Сотрудника id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetEmployeeByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Сотрудника {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override Employee GetEntity(SqlDataReader sqlDataReader)
        {
            Employee employee = new Employee();
            IRepository<Address> adoNetRepositoryAddress = new ADONetRepositoryAddress();
            employee.Id = (int)sqlDataReader["Id"];
            employee.Surname = sqlDataReader["Surname"].ToString();
            employee.Name = sqlDataReader["Name"].ToString();
            employee.Middlename = sqlDataReader["Middlename"].ToString();
            employee.Post = sqlDataReader["Post"].ToString();
            employee.Address = adoNetRepositoryAddress.GetById((int)sqlDataReader["AddressId"]);
            return employee;
        }
    }
}
